package com.xc.hotelmgr.webapi.modules.sys.service.impl;

import com.xc.hotelmgr.webapi.modules.sys.service.MqInstance;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class MqInstanceImpl implements MqInstance {

    private final DiscoveryClient discoveryClient;

    @Override
    public List<URI> getInstance(String name) {
        List<ServiceInstance> instances = discoveryClient.getInstances(name);
        List<URI> uris = new ArrayList<>();
        for (ServiceInstance s:instances) {
            URI uri = s.getUri();
            uris.add(uri);
        }
        return uris;
    }
}
